package controller;

import beans.HistoryTask;
import beans.RunningTask;
import dao.HistoryTaskDao;
import dao.JobDao;
import engine.BIEngine0_1.TaskThread;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author zhuyaokunll@foxmail.com
 * @date 2018/9/21 11:08
 */
@SuppressWarnings("Duplicates")
public class BriefTaskState extends HttpServlet {
    private static final long serialVersionUID = 4671758619887487106L;

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String taskHash = request.getParameter("taskHash");
        TaskThread task = RunningTask.TASKMAP.getTask(taskHash);
        List<Object> taskHashes = new HistoryTaskDao().getSubTaskHashByBatchTaskHash(taskHash).getDatas();
        JSONObject result = new JSONObject();
        HistoryTaskDao taskDao = new HistoryTaskDao();
        HistoryTask historyTask = taskDao.getHistoryTaskByTaskHash(taskHash);
        //2018-7-23添加，根据所有子任务的状态来判断批量任务的状态：finished、stopped、pause、running
        // 有一个暂停pause，整体暂停；
        // 有一个暂停stopped，整体暂停；
        // 有一个运行中running，整体运行中；
        // 全部完成finished，整体完成；
        int state = -1;
        boolean isFinished = true;
        JSONArray jobState = new JobDao().getBatchTaskPackedJobState(taskHash,true);
        for(int i=0;i<jobState.size();i++)
        {
            JSONObject subTaskState = jobState.getJSONObject(i);
            JSONArray subJobState = subTaskState.getJSONArray("state");
            for(int j=0;j<subJobState.size();j++)
            {
                if(subJobState.getString(j).equals("pause"))
                {
                    state = 2;
                    isFinished = false;
                    //跳出当前循环
                    break;
                }
                if(subJobState.getString(j).equals("stopped"))
                {
                    state = 3;
                    isFinished = false;
                    break;
                }
                if(subJobState.getString(j).equals("running"))
                {
                    state = 1;
                    isFinished = false;
                    break;
                }
            }
            //跳出全部循环
            if(state!=-1)
                break;
        }
        //2018-7-23更新：1：运行中；2：暂停；3：终止；4：完成
        result.put("taskState", state==-1?4:state);
        result.put("taskName",historyTask.getName());
//        result.put("json", historyTask.getJson());
        result.put("state", taskDao.getHistoryBatchTaskState(historyTask.getTask_hash()));
//        result.put("orders",historyTask.getAlgorithmOrders());
        result.put("jobState",jobState);
        result.put("subTaskHashes",taskDao.getSubTaskHashByBatchTaskHash(historyTask.getTask_hash()).getDatas());
        response.getWriter().write(result.toString());
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);

    }
}
